import datetime

from sqlalchemy import Column, String, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from init.mysql import engine

Base = declarative_base()


class StockUpDown(Base):
    __tablename__ = 'stock_up_down'
    id = Column(Integer, primary_key=True, autoincrement=True)
    qdate = Column(String(20))
    totalUp = Column(Integer)
    totalDown = Column(Integer)
    zt = Column(Integer)
    dt = Column(Integer)
    up8ToZt = Column(Integer)
    up6To8 = Column(Integer)
    up4To6 = Column(Integer)
    up2To4 = Column(Integer)
    up0To2 = Column(Integer)
    down0To2 = Column(Integer)
    down2To4 = Column(Integer)
    down4To6 = Column(Integer)
    down6To8 = Column(Integer)
    down8ToDt = Column(Integer)

    def __init__(self, qdate, totalUp, totalDown, zt, dt, up8ToZt, up6To8, up4To6, up2To4, up0To2, down0To2, down2To4,
                 down4To6, down6To8, down8ToDt):
        self.qdate = qdate
        self.totalUp = totalUp
        self.totalDown = totalDown
        self.zt = zt
        self.dt = dt
        self.up8ToZt = up8ToZt
        self.up6To8 = up6To8
        self.up4To6 = up4To6
        self.up2To4 = up2To4
        self.up0To2 = up0To2
        self.down0To2 = down0To2
        self.down2To4 = down2To4
        self.down4To6 = down4To6
        self.down6To8 = down6To8
        self.down8ToDt = down8ToDt

    @staticmethod
    def insert_or_update(data):
        # 获取当前时间
        now = datetime.datetime.now()
        # 格式化时间为字符串
        formatted_time = now.strftime("%Y-%m-%d %H:%M")
        session = sessionmaker(bind=engine)()
        existing_stock_take_history = session.query(StockUpDown).filter_by(qdate=formatted_time).first()
        if existing_stock_take_history:
            existing_stock_take_history.qdate = formatted_time
            existing_stock_take_history.totalUp = data['totalUp']
            existing_stock_take_history.totalDown = data['totalDown']
            existing_stock_take_history.zt = data['zt']
            existing_stock_take_history.dt = data['dt']
            existing_stock_take_history.up8ToZt = data['up8ToZt']
            existing_stock_take_history.up6To8 = data['up6To8']
            existing_stock_take_history.up4To6 = data['up4To6']
            existing_stock_take_history.up2To4 = data['up2To4']
            existing_stock_take_history.up0To2 = data['up0To2']
            existing_stock_take_history.down0To2 = data['down0To2']
            existing_stock_take_history.down2To4 = data['down2To4']
            existing_stock_take_history.down4To6 = data['down4To6']
            existing_stock_take_history.down6To8 = data['down6To8']
            existing_stock_take_history.down8ToDt = data['down8ToDt']
        else:
            session.add(StockUpDown(qdate=formatted_time, totalUp=data['totalUp'], totalDown=data['totalDown'],
                                             zt=data['zt'], dt=data['dt'], up8ToZt=data['up8ToZt'],
                                             up6To8=data['up6To8'], up4To6=data['up4To6'], up2To4=data['up2To4'],
                                             up0To2=data['up0To2'], down0To2=data['down0To2'],
                                             down2To4=data['down2To4'], down4To6=data['down4To6'],
                                             down6To8=data['down6To8'], down8ToDt=data['down8ToDt']))
        session.commit()


Base.metadata.create_all(engine)
